package cn.myjerry.test.ORM;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 测试使用Map 封装一条数据
 * 使用List<Map<String, Object>>存储多条数据
 * @author Jerry
 *
 */
public class Demo02 {
	public static void main(String[] args) {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet res = null;
		List<Map<String, Object>> list = new ArrayList<>();

		try {
			conn = JDBCUtil.getMysqlConn();
			ps = conn.prepareStatement("SELECT Name,Salary,Age FROM Employee WHERE Id > ?");
			ps.setInt(1, 0);
			res = ps.executeQuery();
			while (res.next()) {
				Map<String, Object> map = new HashMap<>();

				map.put("Name", res.getString("Name"));
				map.put("Salary", res.getDouble("Salary"));
				map.put("Age", res.getInt("Age"));
				
				list.add(map);
			}

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(res, ps, conn);
		}
		
		for(Map<String, Object> map:list) {
			System.out.println(map.toString());
		}
	}
}
